import httplib, urllib, json


#http://localhost:8983/solr/select/?indent=on&q=video&fl=name,id
class solr_conn:

    def __init__(self):
        self.create()
    def create(self):
        self.conn = httplib.HTTPConnection("localhost",8983)
    def create_close(self):
        self.conn.close()
        self.create()

    def do_update(self,data):
        sjson= json.dumps(data)
        print sjson
        self.conn.request("POST", "/solr/update/json?commit=true",body=sjson)
        r2 = self.conn.getresponse()
        print r2

        #return json.loads(response.read())

    def do_search(self,value='*',field='*',dist='5'):
        value = urllib.urlencode({'q':
            ('%(field)s:"%(value)s"~%(dist)s' % locals()),'wt':'json'})
#        q=%(field)s%%3A"%(value)s"~%(dist)s
        self.conn.request("GET",
                '/solr/select/?%(value)s' % locals())
        res = self.conn.getresponse()
        return res.read()

data = {"add": {
          "doc": {
              'id':'mon',
              'text':'monsters',
              'verse':1
              #"id" : "3978-1423103349",
    #    "cat" : ["book","paperback"],
    #"title" : "MYThe Sea of Monsters",
    #"author" : "Rick Riordan",
    #"series_t" : "Percy Jackson and the Olympians",
    #"sequence_i" : 2,
    #"genre_s" : "fantasy",
    #"inStock" : True,
    #"price" : 6.49,
    #"pages_i" : 304
    }
    }

    }

c = solr_conn()
print c.do_update(data)
#print c.do_search(field='text',value='monsters')
